home *** CD-ROM | disk | FTP | other *** search
- REALASM1.DOC
-
- Download REALASM1.ZIP from Canada Remote Systems(CRS).
-
- These functions are targeted towards those who wish to write
- a program in assembler and require floating point precision.
-
- They were not designed to be added to a C program, since
- most C compilers come with <math.h> functions.
-
- In writing assembler programs MODELs, small large, huge,
- ecetera, are NOT required. Someone once said, way back
- when we were limited to 640k "If your code is larger than
- 64k, there is something wrong with your programming style"
- This axiom stands true today - for assembler programs.
-
- Part of the challenge of writing stand alone assembler
- programs is that you have to reinvent the wheel. For
- example, a heap manager, doubly-linked-lists, recursive
- graphics fill, windowing screen editor routines have been
- written by the author, but are certainly not easy to write.
-
- I also often write programs in C and rarely supplement
- these programs with assember functions since C compilers
- are so smart (e.g. Borland C++ version 3.1).
-
-
- Floating point math functions using Microsoft MASM 6.00b
- --------------------------------------------------------
-
- I have been writing in programs in machine code for many years,
- even before the IBM PC XT made its debut. It is a lot easier now
- that we have Microsoft MASM and Borland TASM but both these fine
- companies have closely guarded their floating point emulators.
-
- Microsoft provides examples of integer math, quadword multiplication
- for example in their 'examples' that come with the assembler and in
- the MASM 6.0 manual they devote a whole chapter to floating point
- math but using the coprocessor, of course.
-
- My inspiration was "Small Assembler - An 80x86 Macro Assembler
- Written in Small C" by James E. Hendrix, published by M&T Books.
- It comes with a disk, containing all the source code. His floating
- point math is of course written in C.
-
- Before going any further, we must understand how the math
- coprocessor functions. The temporary real format is
- reviewed. This is the 80-bit format known as a REAL10
- in masm 6.0 jargon:
-
-
- 79 78 64 63 0
- ------ ---------- -------------
- | sign | exponent | significand |
- | 1 | 15 | 64 |
- ------ ---------- -------------
- ^
- explicit 1
-
- SIGN:
- 0 = positive, 1 = negative
-
- EXPONENT:
- This value determined where the binary-point is, however, it is
- biased with a value of 3FFFh so that all exponent values are positive.
- In order to get the true binary exponent, you must subtract 3FFFh.
-
- There is one exception to this rule: if the REAL10 == 0.0,
- (+0.0 or -0.0), there is no bias.
-
- Biasing allows two REAL10 numbers to be compared as if they were
- binary integers. When comparing them bitwise left to right, starting
- at bit-78, the first bit position that differs, orders the numbers.
-
- SIGNIFICAND:
- In a REAL10 bit-63 is always a one (1). Formats REAL8 (double-precision)
- and REAL4 (single-precision) not discussed here have implicit 1's.
- Thus all REAL10 numbers, also known as 'normalised' numbers range
- between 1 and 2.
-
-
- PRECISION:
- The 80x87 temporary real range is 3.4 * 10**(-4932) to 1.2 * 10**(4932),
- which provides enormous precision far beyond what most people need.
- Intel provides other formats (double, single precision) and suggests
- that when using an 80x87, use of the temporary real value compomises
- the device. Rightly so.
-
- This library provides 64-bit precision, 10**(-19) to 10**(19), for
- example: 1234567890123456789.1234567890123456789
-
- Function FTRANGE returns TRUE if (+64 > exp > -64), FALSE otherwise.
-
-
- EXAMPLES (in hexadecimal):
-
- +1.0 = 3fff 8000 0000 0000 0000 exponent == 0
- +10.0 = 4002 A000 0000 0000 0000 exponent == +3
- pi = 4000 C90F DAA2 2168 C235 exponent == +1
-
- write out the high word of the significand of 10.0 in binary
-
- 1010000000000000
-
- write in the binary point
-
- 1.010000000000000
-
- but the exponent is +3, thus we must write
-
- 1010.000000000000
-
- Try writing out the value of pi. Remember that the first digit
- after the decimal point = 0.5, the next 0.25, ecetera.
-
- -------------------------------------------------
-
- FUNCTIONS (see also MATH.INC)
-
- TRUE equ 1
- FALSE equ 0
-
- MAXEXPONENT equ 64
- MINEXPONENT equ -64
-
- szREAL10 equ size REAL10 ; temporary real
- szREAL8 equ size REAL8 ; double precision (double)
- szREAL4 equ size REAL4 ; single precision (float)
-
- NPB typedef near ptr byte
- NPR10 typedef near ptr REAL10
- NPR8 typedef near ptr REAL8
- NPR4 typedef near ptr REAL4
-
- ;---- MOVX.ASM ---- support routines
-
- movx proto near dst:NPB, src:NPB, wide:WORD
- clrx proto near dst:NPB, wide:WORD
- addx proto near dst:NPB, src:NPB, wide:WORD
- subx proto near dst:NPB, src:NPB, wide:WORD
- lshx proto near dst:NPB, wide:WORD
- rshx proto near dst:NPB, wide:WORD
- cmpx proto near dst:NPB, src:NPB, wide:WORD
- cmpxz proto near dst:NPB, wide:WORD
-
- ;---- MISC.ASM ----
-
- ftzero proto near dst:NPR10
- Returns TRUE if dst == 0.0, disregarding sign
-
- ftequal proto near x:NPR10, y:NPR10
- Returns TRUE if x == y, disregarding sign
-
- ftrange proto near dst:NPR10
- Returns TRUE if (+64 > exp > -64)
-
- ftnormal proto near dst:NPR10
- Normalises a REAL10
-
- ftsign proto near dst:NPR10
- sign ^= 1;
-
- ftcomp proto near x:NPR10, y:NPR10
- Returns: AX = 1, if x > y
- -1, if x < y
- 0, if x == y
-
- ftswap proto near dst:NPR10, src:NPR10
- dst <---> src
-
- LOAD constants
- load0 proto near dst:NPR10 dst = 0.0
- load1 proto near dst:NPR10 dst = 1.0
- load10 proto near dst:NPR10 dst = 10.0
- load10_19 proto near dst:NPR10 dst = 10**19, used in ft2st
- loadpi proto near dst:NPR10 dst = pi
-
- ftadd proto near dst:NPR10, src:NPR10
- dst += src
-
- ftsub proto near dst:NPR10, src:NPR10
- dst -= src
-
- ftmul proto near dst:NPR10, src:NPR10
- dst *= src
- (shift and add multiplication)
-
- ftdiv proto near dst:NPR10, src:NPR10
- dst /= src
- (shift and subtract division)
-
- itoft proto near real:NPR10, integer:SWORD
- Convert an integer into a REAL10
-
- ftaddi proto near real:NPR10, integer:SWORD
- dst += integer
-
- ftsubi proto near real:NPR10, integer:SWORD
- dst -= integer
-
- ftmuli proto near real:NPR10, integer:SWORD
- dst *= integer
-
- ftdivi proto near real:NPR10, integer:SWORD
- dst /= integer
-
- ftmod proto near dst:NPR10, src:NPR10
- Returns dst /= src
- AX = number of subtractions
-
- ftrecip proto near dst:NPR10
- Returns reciprocal of dst
-
- ftpower proto near real:NPR10, power:WORD
- Returns real**power
-
- ftfact proto near real:NPR10, factorial:WORD
- Returns real = factorial
-
- ftsqrt proto near real:NPR10
- Returns square root of real
- (Newton-Raphson approximation, 48 bit precision)
-
- ftsin proto near real:NPR10, radians:NPR10
- Returns real = sin(radians)
- (64-bit accuracy for pi/4 radians)
-
- ftcos proto near real:NPR10, radians:NPR10
- Returns real = cos(radians)
- (64-bit accuracy for pi/4 radians)
-
- ftexp proto near real:NPR10
- Returns real = e**real
-
- st2ft proto near dst:NPR10, string:NPB
- Converts string to a REAL10
-
- st2int proto near string:NPB
- Converts string to an integer
-
- ft2st proto near src:NPR10, string:NPB
- Converts a REAL10 to a string
-
-
-
- ADDENDUM:
-
- These functions are not included at present time. They have been
- written for an Fast Fourier Tranform (fft) with Parzen, Hanning,
- Hamming, Blackman and Welch windowing.
- The power spectrum function has yet to be written.
-
- COMPLEX struct
- real REAL10 ?
- imag REAL10 ?
- COMPLEX ends
- szCOMPLEX equ size COMPLEX
-
- NPCX typedef near ptr COMPLEX
-
- ;---- COMPLEX.ASM ------
-
- complexadd proto near c1:NPCX, c2:NPCX, res:NPCX
- Returns res = c1 + c2
-
- complexsub proto near c1:NPCX, c2:NPCX, res:NPCX
- Returns res = c1 - c2
-
- complexmul proto near c1:NPCX, c2:NPCX, res:NPCX
- Returns res = c1 * c2
-
- complexdiv proto near c1:NPCX, c2:NPCX, res:NPCX
- Returns res = c1 / c2
-
-
- Statistical functions, standard deviation, random number
- generator, ecetera are not presently included.
-
- | alan.illeman@canrem.com | "At school I was in a class of my own, |
- | CRS Toronto, Canada | all the other kids got promoted." |
-